package myproject.computer;

/**
 * @author shkstart
 * @creat 2022-04-16:42
 * 电脑有品牌，颜色，屏幕大小等属性，其中磁盘容量
 */
public class Computer {
    private String brand;
    private String color;
    private String dpi;//分辨率
    private int price;
    private int diskCapacity;//磁盘容量
    private int surplusCapacity;//剩余容量
    private Disk[] disks = new Disk[10];
    private int count;//磁盘分区数量


    public Computer() {
    }

    public Computer(String brand, String color, String dpi, int price, int diskCapacity) {
        this.brand = brand;
        this.color = color;
        this.dpi = dpi;
        this.price = price;
        this.diskCapacity = diskCapacity;
        surplusCapacity = diskCapacity;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getDpi() {
        return dpi;
    }

    public void setDpi(String dpi) {
        this.dpi = dpi;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getDiskCapacity() {
        return diskCapacity;
    }

    public int getSurplusCapacity() {
        return surplusCapacity;
    }

    public void setDiskCapacity(int diskCapacity) {
        this.diskCapacity = diskCapacity;
    }

    public Disk[] getDisks() {
        return disks;
    }

    public void setDisks(Disk[] disks) {
        this.disks = disks;
    }

    //电脑启动
    public boolean start() {
        return true;
    }

    //添加
    public Disk addDisk(String name, int size) {
        return new Disk(size, name);
    }

    //格式化
    public boolean formate(Disk disk) {
        //磁盘对象数组扩容
        if (count > disks.length) {
            Disk[] newDisks = new Disk[disks.length + 5];
            for (int i = 0; i < count; i++) {
                newDisks[i] = disks[i];
            }
            disks = newDisks;
        }

        surplusCapacity -= disk.getCapacity();
        if(surplusCapacity < 0){
            return false;
        }
        disks[count++] = disk;
        return true;
    }

    //安装操作系统
    public boolean addOS(String OSName, String diskName) {
        for (int i = 0; i < count; i++) {
            if (diskName.equals(disks[i].getDiskName())) {
                if (OSName.equals("win7")) {
                    install(diskName,new SoftWare("win7",5000));
                    //System.out.println("安装成功，大小为5000M");
                    return true;
                } else if (OSName.equals("win10")) {
                    install(diskName,new SoftWare("win10",7000));
                    //System.out.println("安装成功，大小为7000M");
                    return true;
                } else if (OSName.equals("win11")) {

                    install(diskName,new SoftWare("win11",9000));
                    //System.out.println("安装成功，大小为9000M");
                    return true;
                } else {
                    System.out.println("没有该操作系统");
                    return false;
                }

            }

        }
        System.out.println("没有该盘符");
        return false;
    }

    /**
     * 功能4：安装普通软件还是下载软件
     * @param num
     * @param wareName
     * @param wareSize
     * @return 返回一个软件对象
     */
    public SoftWare chooseSoftWare(int num, String wareName, int wareSize) {
        if (num == 1) {
            return new SoftWare(wareName, wareSize);
        } else if (num == 2) {
            return new DownloadSoftWare(wareName, wareSize);
        } else {
            return null;
        }
    }

    /**
     * 把软件存放在具体的盘符中
     * @param diskName
     * @param softWare
     */
    public void install(String diskName, SoftWare softWare) {
        for (int i = 0; i < count; i++) {
            if (diskName.equals(disks[i].getDiskName())) {
                disks[i].addSoftware(softWare);
            }
        }

    }

    /**
     * 读取磁盘安装的软件
     *
     * @param diskName
     */
    public void read(String diskName) {
        boolean isFind = false;
        for (int i = 0; i < count; i++) {
            if (diskName.equals(disks[i].getDiskName())) {
                isFind = true;
                disks[i].readDisk();
                System.out.println("剩余空间" + (disks[i].getCapacity() - disks[i].wareOfTotalSize()) + "Mb");
            }
        }
        if (!isFind) {
            System.out.println("没有" + diskName + "这个盘");
        }

    }

    /**
     * 利用下载软件再去下载软件，并且安装到指定盘符中
     * @param name
     * @param wareName
     * @param wareSize
     * @param diskName
     * @return true:安装成功，false:笼统为安装失败
     */
    public boolean download(String name, String wareName, int wareSize, String diskName) {

        for (int i = 0; i < count; i++) {

            boolean a = disks[i].down(name, wareName, wareSize);
            if(!a){
                return false;
            }
            install(diskName,new SoftWare(wareName,wareSize));
            return true;

        }
        return false;

    }

    @Override
    public String toString() {
        return "brand=" + brand +
                ", color=" + color +
                ", dpi=" + dpi +
                ", price=" + price +
                "元, diskCapacity=" + diskCapacity + "Mb";
    }
}
